package com.definity.toolkit.ui.widget;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;

import javax.swing.JPanel;
import javax.swing.border.Border;

import com.definity.toolkit.ui.Body;
import com.definity.toolkit.ui.widget.paint.Paint;

public class BorderPanel extends JPanel implements Widget<BorderPanel>, WidgetPaint<BorderPanel> {

	private static final long serialVersionUID = -4707766450016207258L;

	private Paint paint;
	
	public BorderPanel() {
		setLayout(new BorderLayout());
	}
	
	@Override
	public String id() {
		return getName();
	}
	
	@Override
	public BorderPanel id(String id) {
		setName(id);
		return this;
	}
	
	public BorderPanel top(Component component) {
		add(component, BorderLayout.NORTH);
		return this;
	}

	public BorderPanel left(Component component) {
		add(component, BorderLayout.WEST);
		return this;
	}

	public BorderPanel right(Component component) {
		add(component, BorderLayout.EAST);
		return this;
	}

	public BorderPanel bottom(Component component) {
		add(component, BorderLayout.SOUTH);
		return this;
	}

	public BorderPanel center(Component component) {
		add(component, BorderLayout.CENTER);
		return this;
	}

	@Override
	public BorderPanel properties(Body<BorderPanel> body) {
		body.content(this);
		return this;
	}
	
	@Override
	public BorderPanel size(int width, int height) {
		setPreferredSize(new Dimension(width, height));
		return this;
	}

	@Override
	public Color background() {
		return getBackground();
	}

	@Override
	public BorderPanel background(Color background) {
		setBackground(background);
		return this;
	}

	@Override
	public Color foreground() {
		return getForeground();
	}

	@Override
	public BorderPanel foreground(Color foreground) {
		setForeground(foreground);
		return this;
	}

	@Override
	public Font font() {
		return getFont();
	}

	@Override
	public BorderPanel font(Font font) {
		setFont(font);
		return this;
	}

	public BorderPanel opaque(boolean opaque) {
		setOpaque(opaque);
		return this;
	}

	public Border border() {
		return getBorder();
	}
	
	public BorderPanel border(Border border) {
		setBorder(border);
		return this;
	}

	public BorderPanel paint(Paint paint) {
		this.paint = paint;
		return this;
	}

	@Override
	protected void paintComponent(Graphics g) {
		if (paint != null)
			paint.paint((Graphics2D) g, this);
		super.paintComponent(g);
	}
	
}
